package cn.stazxr.controller;

import cn.stazxr.constants.HttpConstant;
import cn.stazxr.core.TokenHandler;
import cn.stazxr.entity.YdlLoginUser;
import cn.stazxr.exception.BusinessException;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Objects;

/**
 * @author talentestors
 * @version 1.0
 * @since 2025.11.02
 **/
@Controller
public class BaseController {

	@Autowired
	protected TokenHandler tokenHandler;

	protected YdlLoginUser getLoginUser() {
		HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
		String token = request.getHeader(HttpConstant.AUTHORIZATION);
		String userName = request.getHeader("userName");
		if (token == null) {
			throw new BusinessException("未登录");
		}
		// 3. 使用token去 Redis 中查询用户信息
		return tokenHandler.getYdlLoginUser(token, userName);
	}
}
